/**
 * 
 */
package com.jeecms.wicool.dao.question.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.question.QuestionDao;
import com.jeecms.wicool.entity.question.Question;

/**
 * @author spring
 *
 */
public class QuestionDaoImpl extends HibernateBaseDao<Question, Integer>
		implements QuestionDao {

	@Override
	protected Class<Question> getEntityClass() {
		return Question.class;
	}

	@Override
	public Pagination getPage(String title, Integer type, int pageNo,
			int pageSize) {
		 Finder f = Finder.create("select bean from Question bean where bean.status=1");

        if (StringUtils.isNotBlank(title)) {
            f.append(" and bean.title = :title");
            f.setParam("title", title);
        }
        if (type != null) {
            f.append(" and bean.type = :type");
            f.setParam("type", type);
        }
        f.append(" order by bean.id desc");
        return find(f, pageNo, pageSize);
	}

	@Override
	public Question findById(Integer id) {
		return get(id);
	}

	@Override
	public Question save(Question bean) {
		getSession().save(bean);
		getSession().flush();
		getSession().clear();
		return bean;
	}

	@Override
	public Question deleteById(Integer id) {
		Question entity = super.get(id);
        if (entity != null) {
            getSession().delete(entity);
        }
        return entity;
	}

	@Override
	public List<Question> deleteByIds(Integer[] ids) {
		List<Question> list = new ArrayList<Question>();
		for (Integer id : ids) {
			list.add(deleteById(id));
		}
		getSession().flush();
		getSession().clear();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Question> getAll() {
		 Finder f = Finder.create("select bean from Question bean where bean.status=1");
	        f.append(" order by bean.id desc");
	        return find(f);
	}

}
